package com.huawei.secure.android.common.webview;

import android.annotation.TargetApi;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.URLUtil;
import com.huawei.secure.android.common.util.LogsUtil;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class UriUtil {
    private static final String TAG = "UriUtil";

    public static boolean isUrlHostInWhitelist(String str, String[] strArr) {
        if (strArr == null || strArr.length == 0) {
            LogsUtil.e(TAG, "whitelist is null");
            return false;
        }
        for (String isUrlHostMatchWhitelist : strArr) {
            if (isUrlHostMatchWhitelist(str, isUrlHostMatchWhitelist)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isUrlHostSameWhitelist(String str, String str2) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            return TextUtils.equals(getHostByURI(str), d(str2));
        }
        Log.e(TAG, "isUrlHostSameWhitelist: url or host is null");
        return false;
    }

    public static boolean isUrlHostSameWhitelist(String str, String[] strArr) {
        if (strArr == null || strArr.length == 0) {
            LogsUtil.e(TAG, "whitelist is null");
            return false;
        }
        for (String isUrlHostSameWhitelist : strArr) {
            if (isUrlHostSameWhitelist(str, isUrlHostSameWhitelist)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isUrlHostMatchWhitelist(String str, String str2) {
        String hostByURI = getHostByURI(str);
        if (TextUtils.isEmpty(hostByURI) || TextUtils.isEmpty(str2)) {
            LogsUtil.e(TAG, "url or whitelist is null");
            return false;
        }
        String d = d(str2);
        if (TextUtils.isEmpty(d)) {
            Log.e(TAG, "whitelist host is null");
            return false;
        } else if (d.equals(hostByURI)) {
            return true;
        } else {
            if (!hostByURI.endsWith(d)) {
                return false;
            }
            try {
                hostByURI = hostByURI.substring(0, hostByURI.length() - d.length());
                if (hostByURI.endsWith(".")) {
                    return hostByURI.matches("^[A-Za-z0-9.-]+$");
                }
                return false;
            } catch (IndexOutOfBoundsException e) {
                LogsUtil.e(TAG, "IndexOutOfBoundsException" + e.getMessage());
                return false;
            } catch (Exception e2) {
                LogsUtil.e(TAG, "Exception : " + e2.getMessage());
                return false;
            }
        }
    }

    @TargetApi(9)
    public static String getHostByURI(String str) {
        if (TextUtils.isEmpty(str)) {
            LogsUtil.i(TAG, "url is null");
            return str;
        }
        try {
            if (!URLUtil.isNetworkUrl(str)) {
                LogsUtil.e(TAG, "url don't starts with http or https");
                return null;
            } else if (str.contains("{") || str.contains("}") || str.contains("[") || str.contains("]")) {
                return new URL(str.replaceAll("[\\\\#]", "/")).getHost();
            } else {
                return new URI(str).getHost();
            }
        } catch (URISyntaxException e) {
            LogsUtil.e(TAG, "getHostByURI error : " + e.getMessage());
            return null;
        } catch (MalformedURLException e2) {
            LogsUtil.e(TAG, "getHostByURI error  MalformedURLException : " + e2.getMessage());
            return null;
        }
    }

    private static String d(String str) {
        if (!TextUtils.isEmpty(str)) {
            return URLUtil.isNetworkUrl(str) ? getHostByURI(str) : str;
        } else {
            LogsUtil.i(TAG, "whiteListUrl is null");
            return null;
        }
    }

    public static boolean isUrlHostAndPathInWhitelist(String str, String[] strArr) {
        if (strArr == null || strArr.length == 0) {
            LogsUtil.e(TAG, "whitelist is null");
            return false;
        }
        for (String isUrlHostAndPathMatchWhitelist : strArr) {
            if (isUrlHostAndPathMatchWhitelist(str, isUrlHostAndPathMatchWhitelist)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isUrlHostAndPathMatchWhitelist(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return false;
        }
        if (str.contains("..") || str.contains("@")) {
            Log.e(TAG, "url contains unsafe char");
            return false;
        } else if (str2.equals(str) || str.startsWith(str2 + "?") || str.startsWith(str2 + "#")) {
            return true;
        } else {
            if (!str2.endsWith("/")) {
                return false;
            }
            if (Uri.parse(str).getPathSegments().size() - Uri.parse(str2).getPathSegments().size() == 1) {
                return str.startsWith(str2);
            }
            return false;
        }
    }
}
